package com.fortune.app;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * 应用程序主类，用于初始化全局资源和管理应用状态
 */
public class FortuneApplication extends Application {
    private static FortuneApplication instance;
    private SharedPreferences preferences;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        preferences = getSharedPreferences("fortune_preferences", Context.MODE_PRIVATE);
        
        // 初始化数据库
        DatabaseHelper.getInstance(this);
        
        // 初始化通知系统
        NotificationHelper.init(this);
    }

    /**
     * 获取应用实例
     */
    public static FortuneApplication getInstance() {
        return instance;
    }

    /**
     * 获取SharedPreferences
     */
    public SharedPreferences getPreferences() {
        return preferences;
    }

    /**
     * 保存用户信息
     */
    public void saveUserInfo(String birthDate, String gender) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("user_birth_date", birthDate);
        editor.putString("user_gender", gender);
        editor.apply();
    }

    /**
     * 获取用户生日
     */
    public String getUserBirthDate() {
        return preferences.getString("user_birth_date", "");
    }

    /**
     * 获取用户性别
     */
    public String getUserGender() {
        return preferences.getString("user_gender", "");
    }

    /**
     * 检查用户是否已设置个人信息
     */
    public boolean isUserInfoSet() {
        return !getUserBirthDate().isEmpty();
    }

    /**
     * 保存通知设置
     */
    public void saveNotificationSettings(boolean enabled, String time) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("notifications_enabled", enabled);
        editor.putString("notification_time", time);
        editor.apply();
    }

    /**
     * 检查通知是否启用
     */
    public boolean isNotificationsEnabled() {
        return preferences.getBoolean("notifications_enabled", true);
    }

    /**
     * 获取通知时间
     */
    public String getNotificationTime() {
        return preferences.getString("notification_time", "08:00");
    }
}
